//
//  LDBaseManySelTagsView.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/3.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit


enum LDBaseManySelTagsViewAddBtnType:Int {
    case AddBtn = 1
    case Default = 0
}

class LDBaseManySelTagsView: UIView {

    var anchorPoint:CGPoint?
    var SameLabTag = 888
    var sendParamsColsure: ((params:NSMutableArray)->())?
    var totalLabHeight:CGFloat = 35
    var addHightBtn:UIButton?
    internal var limitTags = 0
    // 选择的标签
    var selLabArrParams = NSMutableArray()
    
    var type:LDBaseManySelTagsViewAddBtnType = .Default
    
    //上次选择的值 //0...n
    var preSelLabID = [String]() {
        
        didSet {
            if titleParams.count > 0 {
                self.updataSelLab(self.preSelLabID)
            }
        }
    }
    //判断 根控制器
    internal var control = String()

    var titleParams = [String]() {
        
        didSet{
            
            performSelectorOnMainThread(#selector(setup), withObject: self, waitUntilDone: true)
        }
    }
    
    var tapcosure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = LDGolbalColor.BgGoundGray
    }
    
    func setup() {
        
        self.setDefaultContent()
        self.updataSelLab(self.preSelLabID)
    }
    
    //更新选择按钮
    func updataSelLab(arr:Array<String>) {
        
        for item in arr {
            
            if titleParams.count == 0 {
                return
            }
            let id = Int(item)
            let labs = viewWithTag(SameLabTag + id!) as! UILabel
            labs.backgroundColor = LDGolbalColor.GreenAssist
            labs.textColor = LDGolbalColor.White
            labs.layer.cornerRadius = 3
            labs.layer.masksToBounds = true
            labs.layer.borderWidth = 0
            labs.enabled = true
            self.layoutIfNeeded()
            selLabArrParams.addObject(item)
        }
    }
    
    func setDefaultContent() {
        
        frame = LDFrame.ScreenBounds
        var newtitleParams:Array<String> = titleParams
        newtitleParams.append("+++++")
        
        let count:Int = newtitleParams.count
        
        var labX:CGFloat = Margin_V*2
        var labY:CGFloat = Margin_V*3
        var totalW:CGFloat = Margin_V*4
        
        let labH:CGFloat = 35
        
        for i in 0..<count {
            
            let lab = UILabel.setDetailsLab(newtitleParams[i], fontSize: 13, titleColor: LDFontColor.DeepGray, Alignment: .Center)
            
            if anchorPoint != nil {
                lab.frame.origin.x = anchorPoint!.x
                lab.frame.origin.y = anchorPoint!.y
            }
            
            addSubview(lab)
            
            lab.backgroundColor = LDGolbalColor.White
            lab.layer.cornerRadius = 3
            lab.layer.borderWidth = 1
            lab.layer.borderColor = LDIconColor.LightGray.CGColor
            lab.userInteractionEnabled = true
            
            lab.enabled = false
            lab.tag = SameLabTag + i
            
            lab.bk_whenTapped({ [weak self] in
                self?.sendNoti(i, tagTitle: newtitleParams[i], control: "nil", lab: lab)
            })
            
            let rect = NSString.init(string: newtitleParams[i]).boundingRectWithSize(CGSizeMake(300, 13), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            
            let labW = rect.width + LDMargin_H*2
            totalW += labW + LDMargin_H*2
            
            if totalW > (self.width) {
                totalW = Margin_H*4 + labW
                labX = Margin_V*2
                labY = labY + labH + LDMargin_V*3
            }
            
            lab.frame = CGRectMake(labX, labY, labW, labH)
            labX = labX + labW + LDMargin_H*2
            
            
            if (i == count-1){
                
                if type == .Default {
                    
                    totalLabHeight = CGRectGetMaxY(lab.frame) + LDMargin_V*3
                    lab.hidden = true
                    lab.frame = CGRectZero//CGRectMake(0, 0, LDFrame.ScreenWidth, totalLabHeight)
                    
                } else if type == .AddBtn {
                    
                    addHightBtn = UIButton.setDetailsBtn("＋", picName: nil, btnBackColor: LDGolbalColor.GreenAssist, titleColor: LDGolbalColor.White)
                    addHightBtn?.frame = CGRectMake((lab.x), (lab.y), 65, labH)
                    addSubview(addHightBtn!)
                    lab.hidden = true
                    
                    addHightBtn?.bk_whenTapped({ [weak self] in
                        if self?.tapcosure != nil {
                            self?.tapcosure!() }
                        })
                    
                    totalLabHeight = CGRectGetMaxY(addHightBtn!.frame) + LDMargin_V*3
                    self.frame = CGRectMake(0, 0, LDFrame.ScreenWidth, totalLabHeight)
                }
                
                layoutIfNeeded()
                
            }
        }
    }
    //
    func sendTapAddHightLightBtn(closure:(()->Void)?) {
        
        self.tapcosure = closure
    }
    
    //通知 n控制器
    func sendNoti(index:Int,tagTitle:String,control:String,lab:UILabel) {
        
        if selLabArrParams.count >= limitTags && lab.enabled == false {
        
            showLDAlertView("便签不能大于\(limitTags)个!")
            
            lab.textColor = LDFontColor.DeepGray
            lab.backgroundColor = LDGolbalColor.White
            return
        }
        
        lab.enabled = !lab.enabled
        let dict = "\(index)"
        
        if lab.enabled {
            lab.layer.masksToBounds = true
            lab.backgroundColor = LDGolbalColor.GreenAssist
            lab.textColor = LDGolbalColor.White
            
            if selLabArrParams.containsObject(dict) == false {
                selLabArrParams.addObject(dict)
            }
            
        } else {
            
            lab.backgroundColor = LDGolbalColor.White
            lab.textColor = LDFontColor.DeepGray
            
            if selLabArrParams.containsObject(dict) == true {
                selLabArrParams.removeObject(dict)
            }
        }
        
        debugPrint(selLabArrParams.count,"selLabArrParams.count")
        
        if sendParamsColsure != nil {
            sendParamsColsure!(params: selLabArrParams)
        }
    }
    
    func sendManyLabsParams(colsure:(params:NSMutableArray)->()) {
        sendParamsColsure = colsure
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
